﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
            panel1.MouseClick += Panel1_MouseClick;
        }
        // 定义自定义坐标系的范围
        int customMinX = 0;
        int customMaxX = 360;
        int customMinY = 0;
        int customMaxY = 255;
        // 获取Panel的宽度和高度
        int panelWidth = 0;
        int panelHeight = 0;//相当于实际上就是panel1.Height-20个像素
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen axisPen = new Pen(Color.Black);
            Pen axisPen1 = new Pen(Color.Red);
            Pen gridPen = new Pen(Color.LightGray);
            Brush textBrush = Brushes.Black;
            Font textFont = new Font("Arial", 8);

            panelWidth = panel1.Width - 40;
            panelHeight = panel1.Height;//相当于实际上就是panel1.Height-20个像素
            Console.WriteLine(panel1.Height);



            // 绘制X轴和Y轴
            //g.DrawLine(axisPen, 20, panelHeight, panelWidth + 20, panelHeight); // X轴


            // 绘制X轴刻度
            //for (int i = 0; i <= 10; i++)
            //{
            //    int x = (int)((double)i / 10 * panelWidth); // 减1是为了避免超出Panel边界
            //    int customX = customMinX + (int)((double)i / 10 * (customMaxX - customMinX));
            //    g.DrawLine(gridPen, x + 20, panelHeight, x + 20, panelHeight - 6); // 减6是为了让刻度线短一点
            //    g.DrawString(customX.ToString(), textFont, textBrush, x + 13, panelHeight);
            //}
            g.DrawLine(axisPen1, 20, panelHeight, 20, 20); // Y轴
            //// 绘制Y轴刻度
            //for (int i = 0; i <= 10; i++)
            //{
            //    int y = panelHeight - (int)((double)i / 10 * (panelHeight - 20)); // 减1是为了避免超出Panel边界
            //    int customY = customMinY + (int)((double)i / 10 * (customMaxY - customMinY));
            //    g.DrawLine(gridPen, 20, y, 25, y);
            //    //g.DrawString(customY.ToString(), textFont, textBrush, 20-5, y - 7);
            //    g.DrawString(customY.ToString(), textFont, textBrush, 0, y - 7);
            //}
            //DrawCustomLine();


        }
        // 将自定义X坐标映射到屏幕坐标
        private int MapXToScreen(int customX, int panelWidth)
        {
            //return ((int)((float)customX / (customMaxX - customMinX) * panelWidth)) + 230;



            //return (int)((float)customX / (customMaxX - customMinX) * panelWidth);

            return (int)(((customX - customMinX) * (float)(panelWidth - 20) / (customMaxX - customMinX)) + 20);
            //return (int)screenXFloat;
        }

        // 将自定义Y坐标映射到屏幕坐标
        private int MapYToScreen(int customY, int panelHeight)
        {
            //return (int)((float)(customMaxY - customY) / (customMaxY - customMinY) * panelHeight);
            return (int)(((customMaxY - customY) * (float)(panelHeight - 20) / (customMaxY - customMinY)) - 20);
        }

        // 将屏幕X坐标映射到自定义坐标
        private int MapScreenToX(int screenX, int panelWidth)
        {
            return (int)(Math.Round((decimal)(screenX - 20) / (panelWidth - 20),1) * (customMaxX - customMinX)) + customMinX;
        }

        // 将屏幕Y坐标映射到自定义坐标
        private int MapScreenToY(int screenY, int panelHeight)
        {
            return customMaxY - (int)((float)(screenY - 20) / (panelHeight - 20) * (customMaxY - customMinY));
        }

        // Panel控件的点击事件处理程序
        private void Panel1_MouseClick(object sender, MouseEventArgs e)
        {
            // 将屏幕坐标映射到自定义坐标系中
            int customX = MapScreenToX(e.X, panelWidth);
            int customY = MapScreenToY(e.Y, panelHeight);

            MessageBox.Show($"点击坐标：X={e.X}, Y={e.Y}\n对应自定义坐标：X={customX}, Y={customY}");
        }
        // 绘制指定线段
        private void DrawCustomLine()
        {
            using (Graphics g = panel1.CreateGraphics())
            {
                // 定义线段坐标
                Point[] customPoints = new Point[]
                {
                    new Point(230, 0),
                    new Point(280, 20),
                    new Point(300, 70),
                    new Point(315, 90)
                };

                // 转换自定义坐标为屏幕坐标
                Point[] screenPoints = new Point[customPoints.Length];
                for (int i = 0; i < customPoints.Length; i++)
                {
                    Console.WriteLine($"自定义坐标为{customPoints[i].X}，转化后坐标为{MapXToScreen(customPoints[i].X, panel1.Width)}");
                    screenPoints[i] = new Point(MapXToScreen(customPoints[i].X, panel1.Width), MapYToScreen(customPoints[i].Y, panel1.Height));
                }

                // 绘制线段
                g.DrawLines(Pens.Red, screenPoints);
            }
        }
    }
}
